package com.huawei.hgip.paintgame.util;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.huawei.hgip.paintgame.m.data.Menu;

/**
 * 对数据库的一些具体的操作
 * 
 * @author Administrator
 * 
 */
public class DBAdapter {
	private DatabaseHelper DBHelper;
	private Context context;
	private SQLiteDatabase db;

	public DBAdapter(Context ctx) {
		this.context = ctx;
		DBHelper = new DatabaseHelper(context);
	}

	// singer
	private static DBAdapter instance;

	public static DBAdapter getInstance(Context context) {
		if (instance == null && context != null) {
			instance = new DBAdapter(context);
		}
		return instance;
	}

	private DBAdapter open() {
		db = DBHelper.getWritableDatabase();
		return this;
	}

	private void close() {
		DBHelper.close();
	}

	/**
	 * 获取主页面菜单列表
	 * 
	 * @return
	 */
	public ArrayList<Menu> getShowMenuList() {
		ArrayList<Menu> menuList = new ArrayList<Menu>();
		open();
		Cursor cur = null;
		try {
			cur = db.query(DBHelper.TABLE_MENU, new String[] { DBHelper.ID,
					DBHelper.APP_NAME_STR, DBHelper.APP_LINK_STR,
					DBHelper.APP_ICON_URL_STR, DBHelper.APP_STATES,
					DBHelper.APP_CLICK_TIMES }, null, null, null, null,
				DBHelper.APP_CLICK_TIMES + " DESC");
		} catch (Exception e) {
			close();
			return menuList;
		}

		cur.moveToFirst();
		for (int i = 0; i < cur.getCount(); i++) {
			if (cur.getInt(4) == 0) {
				Menu menu = new Menu();
				menu.setId(cur.getInt(0));// id
				menu.setName(cur.getString(1));// name
				menu.setAction(cur.getString(2));// action
				menu.setImgurl(cur.getString(3));// img
				menu.setShow(cur.getInt(4) == 0);// states
				menu.setClickTimes(cur.getInt(5));
				menuList.add(menu);
			}
			cur.moveToNext();
		}

		cur.close();
		close();

		return menuList;
	}

	public ArrayList<Menu> getAllMenuList() {
		ArrayList<Menu> menuList = new ArrayList<Menu>();
		open();
		Cursor cur = null;
		try {
			cur = db.query(DBHelper.TABLE_MENU, new String[] { DBHelper.ID,
					DBHelper.APP_NAME_STR, DBHelper.APP_LINK_STR,
					DBHelper.APP_ICON_URL_STR, DBHelper.APP_STATES,
					DBHelper.APP_CLICK_TIMES }, null, null, null, null, null);
		} catch (Exception e) {
			close();
			return menuList;
		}

		cur.moveToFirst();
		for (int i = 0; i < cur.getCount(); i++) {
			Menu menu = new Menu();
			menu.setId(cur.getInt(0));// id
			menu.setName(cur.getString(1));// name
			menu.setAction(cur.getString(2));// action
			menu.setImgurl(cur.getString(3));// img
			menu.setShow(cur.getInt(4) == 0);// states
			menu.setClickTimes(cur.getInt(5));
			menuList.add(menu);
			cur.moveToNext();
		}
		close();
		return menuList;
	}

	public void updateAppClickTimes(int appId, int newClickTimes) {
		open();
		ContentValues values = new ContentValues();
		values.put(DBHelper.APP_CLICK_TIMES, newClickTimes);
		db.update(DBHelper.TABLE_MENU, values, DBHelper.ID + "=" + appId, null);
		close();
	}

	public void changeStates(int appId, boolean oldStates) {
		open();
		ContentValues values = new ContentValues();
		values.put(DBHelper.APP_STATES, oldStates ? 1 : 0);
		db.update(DBHelper.TABLE_MENU, values, DBHelper.ID + "=" + appId, null);
		close();
	}
	
	public void insertMenu(Menu menu) {
		open();
		ContentValues values = new ContentValues();
		values.put(DBHelper.ID, menu.getId());
		values.put(DBHelper.APP_NAME_STR, menu.getName());
		values.put(DBHelper.APP_LINK_STR, menu.getAction());
		values.put(DBHelper.APP_ICON_URL_STR, menu.getImgurl());
		values.put(DBHelper.APP_STATES, menu.isShow() ? 0 : 1);
		values.put(DBHelper.APP_CLICK_TIMES, menu.getClickTimes());
		db.insert(DBHelper.TABLE_MENU, DBHelper.APP_NAME_STR, values);
		close();
	}

}
